﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Uber.Storage;
using Uber.Messages;
using Uber.HabboHotel.GameClients;
using Uber.HabboHotel.Pathfinding;
using Uber.HabboHotel.Rooms;

namespace Uber.HabboHotel.Items.Interactors
{
    class InteractorOneWayGate : FurniInteractor
    {
        public override bool OnPlace(GameClient Session, RoomItem Item)
        {
            Item.ExtraData = "0";
            bool hasPaidGate = false;

            if (Item.BaseItem == 992)
            {
                using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                {
                    DataRow Row = dbClient.ReadDataRow("SELECT * FROM rooms WHERE id = '" + Item.GetRoom().RoomId + "' LIMIT 1");
                    UberEnvironment.GetLogging().WriteLine(Row["hasPaidGate"].ToString());
                    try { hasPaidGate = Convert.ToBoolean(Convert.ToInt32(Row["hasPaidGate"])); }
                    catch (Exception e) { UberEnvironment.GetLogging().WriteLine("Gate Error: " + e.Message); }
                    if (hasPaidGate && (Item.Id != Convert.ToInt32(Row["paidGateId"])))
                    {
                        Item.GetRoom().RemoveFurniture(Session, Item.Id, false); 
                        Session.GetHabbo().GetInventoryComponent().AddItem(Item.Id, Item.BaseItem, Item.ExtraData, false);
                        Session.GetHabbo().GetInventoryComponent().UpdateItems(true);
                        Session.SendNotif("There is already a paid gate in this room");
                        return false;
                    }
                    else dbClient.ExecuteQuery("UPDATE rooms SET hasPaidGate='1', paidGateId='" + Item.Id + "' WHERE id='" + Item.RoomId + "'");
                }

            }
            else {
                UberEnvironment.GetLogging().WriteLine(Item.Id.ToString());
            }

            if (Item.InteractingUser != 0)
            {
                RoomUser User = Item.GetRoom().GetRoomUserByHabbo(Item.InteractingUser);

                if (User != null)
                {
                    User.ClearMovement(true);
                    User.UnlockWalking();
                }

                Item.InteractingUser = 0;
            }
            return true;
        }

        public override void OnRemove(GameClient Session, RoomItem Item)
        {
            Item.ExtraData = "0";

            if (Item.BaseItem == 992)
            {
                using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                {
                    dbClient.ExecuteQuery("UPDATE rooms SET hasPaidGate='0', paidGateId='0' WHERE id='" + Item.RoomId + "'");
                }
            }

            if (Item.InteractingUser != 0)
            {
                RoomUser User = Item.GetRoom().GetRoomUserByHabbo(Item.InteractingUser);

                if (User != null)
                {
                    User.ClearMovement(true);
                    User.UnlockWalking();
                }

                Item.InteractingUser = 0;
            }
        }

        public override void OnTrigger(GameClient Session, RoomItem Item, int Request, bool UserHasRights)
        {
            RoomUser User = Item.GetRoom().GetRoomUserByHabbo(Session.GetHabbo().Id);

            if (User == null)
            {
                return;
            }

            if (User.Coordinate != Item.SquareInFront && User.CanWalk)
            {
                User.MoveTo(Item.SquareInFront);
                return;
            }

            if (!Item.GetRoom().CanWalk(Item.SquareBehind.x, Item.SquareBehind.y, Item.Z, true, Session))
            {
                return;
            }

            if (Item.InteractingUser == 0)
            {
                Item.InteractingUser = User.HabboId;

                //User.CanWalk = false;

                if (Item.BaseItem == 992)
                {
                    using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                    {
                        int credits = 20;
                        try { credits = Convert.ToInt32(dbClient.ReadDataRow("SELECT * FROM rooms WHERE id='" + Item.RoomId + "'")["paidGateAmount"]); }
                        catch { }
                        User.GetClient().GetHabbo().Credits -= credits;
                        if (User.GetClient().GetHabbo().Credits < 0)
                        {
                            User.GetClient().SendNotif("You not afford " + credits + " credits!");
                            User.GetClient().GetHabbo().Credits += credits;
                            User.CanWalk = true;
                            return;
                        }
                        else
                        {
                            GameClient gc = UberEnvironment.GetGame().GetClientManager().GetClientByHabbo(UberEnvironment.GetGame().GetClientManager().GetIdByName(Item.GetRoom().Owner));
                            if (gc.GetHabbo().Username != User.GetClient().GetHabbo().Username)
                            {
                                User.GetClient().SendNotif("You were charged " + credits + " credits!");
                                User.GetClient().GetHabbo().UpdateCreditsBalance(true);
                                gc.GetHabbo().Credits += (int)Math.Floor(credits * .75);
                                gc.GetHabbo().UpdateCreditsBalance(true);
                            }
                            else
                            {
                                User.GetClient().SendNotif("You weren't charged as it is your room!");
                            }
                        }
                    }
                }
                /*else
                {
                    UberEnvironment.GetLogging().WriteLine(Item.Id.ToString());
                    GameClient gc = UberEnvironment.GetGame().GetClientManager().GetClientByHabbo(UberEnvironment.GetGame().GetClientManager().GetIdByName(Item.GetRoom().Owner));
                    gc.GetHabbo().Credits += 2;
                    gc.GetHabbo().UpdateCreditsBalance(true);
                }*/

                if (User.IsWalking && (User.GoalX != Item.SquareInFront.x || User.GoalY != Item.SquareInFront.y))
                {
                    User.ClearMovement(true);
                }

                User.AllowOverride = true;
                //User.MoveTo(Item.SquareBehind);
                User.X = Item.SquareBehind.x;
                User.Y = Item.SquareBehind.y;
                User.MoveTo(Item.SquareBehind);
                //Item.ReqUpdate(3);
                Item.InteractingUser = 0;
            }
        }
    }
}
